package com.example.tnot.Teacher.CoursePart.CourseInternal.ClassDetail.VpFragment.AddChapter;

import static android.app.Activity.RESULT_OK;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.tnot.R;

import org.w3c.dom.Text;

public class AddChapterFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private TextView tv_preview;
    private LinearLayout ll_uploadCover,ll_selectClass;
    private EditText et_chapter_name,et_teacher;
    private ImageView iv_back;
    private String mParam1;
    private String mParam2;
    private static final int REQUEST_CODE_PICK_IMAGE = 1;
    Uri selectedImageUri;


    private View view;
    public AddChapterFragment() {
    }

    public static AddChapterFragment newInstance(String param1, String param2) {
        AddChapterFragment fragment = new AddChapterFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.course_add_chapter, container, false);
        tv_preview = view.findViewById(R.id.tv_preview);
        tv_preview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //章节名称
                String chapter_name = et_chapter_name.getText().toString();
                //章节讲师名称
                String teacher = et_teacher.getText().toString();
                NavController navController = Navigation.findNavController(v);
                navController.navigate(R.id.action_addChapterFragment_to_chapterPreviewFragment);
            }
        });
        ll_uploadCover = view.findViewById(R.id.ll_uploadCover);
        ll_uploadCover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
            }
        });
        ll_selectClass = view.findViewById(R.id.ll_selectClass);
        ll_selectClass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到选择班级页面
                NavController navController = Navigation.findNavController(v);
                navController.navigate(R.id.action_addChapterFragment_to_selectClassFragment);
            }
        });


        iv_back = view.findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                navController.navigateUp();
            }
        });
        return view;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode, data);

        if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == RESULT_OK && data != null) {
            selectedImageUri = data.getData();

            //获取手机相册并设置预览
            // 使用 Glide 加载图片到 ImageView
            ImageView imageView = view.findViewById(R.id.iv_cover);


            Glide.with(this)
                    .load(selectedImageUri)
                    .into(imageView);
        }
    }
}